﻿using System;
using System.Threading;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace Athena
{
    public partial class Fpsware : CombatRoutine
    {

        // ************************************************************************************
        public string Environment;
        private bool _isCCLoaded;
        private static LocalPlayer Me { get { return ObjectManager.Me; } }
        private static WoWUnit CT { get { return Me.CurrentTarget; } }
        public override string Name { get { return String.Format("{0}({1}) by {2}", CCName, _versionNumber, AuthorName); } }
        public override WoWClass Class { get { return CCClass; } }
        // ************************************************************************************

        #region CC Constructor
        public Fpsware()
        {
            // If you are not this class don't continue past this point. 
            if (Me.Class != CCClass) return;

            // Register the events of the Stop / Start buttons in HB
            BotEvents.OnBotStarted += BotEvents_OnBotStarted;
            BotEvents.OnBotStopped += BotEvents_OnBotStopped;

            string environment = Utils.IsBattleground ? "PVP" : "PVE";
            environment = ObjectManager.Me.IsInInstance ? "Instance" : environment;
            ConfigSettings.CurrentEnvironment = environment;

            LoadSettings(true);
        }
        #endregion

        #region Configuration Form Stuff
        // All very straight forward here. The name of the UI form is called UIForm
        private static Thread Thread { get; set; }
        private UIForm2 _gui;

        // This overrides HB OnButtonPress event. This where we assign the form to a thread and start the thread
        public override void OnButtonPress()
        {
            Thread = new Thread(OpenGui) {IsBackground = true, Name = "GuiThread"};
            Thread.Start();
        }
        public void OpenGui()
        {
            _gui = new UIForm2(); _gui.ShowDialog();
        }

        public override bool WantButton
        {
            get { return Settings.ShowUI.Contains("always"); }
        }
        #endregion

        #region Load Settings
        private void LoadSettings(bool silent)
        {
            if (!silent) Utils.Log("Loading settings ...");

            Settings.Load();

            if (!silent) Utils.Log(!_isCCLoaded ? "Settings loaded" : "Settings have been updated with your changes", Utils.Colour("DarkRed"));
            Settings.DirtyData = false;

        }
        #endregion
    }
}
